package services;




import java.text.DateFormat;
import java.util.Date;
import java.util.Properties;

import org.jclouds.ContextBuilder;
import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.blobstore.InputStreamMap;
import org.jclouds.openstack.keystone.v2_0.config.CredentialTypes;
import org.jclouds.openstack.keystone.v2_0.config.KeystoneProperties;

import model.Client;
import model.Payment;
import model.Product;


public class StorageHPService {
	
	public boolean store(Client c, Product p){
		
		Properties props=new Properties();
		props.setProperty(KeystoneProperties.CREDENTIAL_TYPE, CredentialTypes.API_ACCESS_KEY_CREDENTIALS);
		
		BlobStoreContext context=ContextBuilder.newBuilder("hpcloud-objectstorage").
				credentials("andregustavoo-tenant1:WU7UC9PHMZR5HCJNX8TA", "tbVCFfpoVTQjRp7YjjJwz0C5lOpI0wrOg0HYptH1").overrides(props).build(BlobStoreContext.class);
		
		//BlobStoreContext context = new BlobStoreContextFactory().createContext("hpcloud-objectstorage", "WU7UC9PHMZR5HCJNX8TA", "tbVCFfpoVTQjRp7YjjJwz0C5lOpI0wrOg0HYptH1");
		
		InputStreamMap input= context.createInputStreamMap("hw");
		
		Payment payment=new Payment();
		
		payment.setClient(c);
		payment.setIdPurchase(p.getProductId());
		payment.setCreditCard(c.getCreditNumber());
		payment.setValue(p.getValue());
		
		String conteudo="";
		
		conteudo += "Dados da compra:" + "\n";
		conteudo += "ID do cliente: " + payment.getClient().getClientId() + "\n";
		conteudo += "ID da compra: " + payment.getIdPurchase() + "\n";
		conteudo += "Data de pagamento: " + DateFormat.getInstance().format(new Date(System.currentTimeMillis())) + "\n";
		conteudo += "Valor pago: " + payment.getValue();
		
		String nomeArquivo = payment.getIdPurchase() + ".txt";
		
		input.putString(nomeArquivo, conteudo);
		
		context.close();
				
		return true;
	}

}
